<template>
  <!--三键下单-->
  <div>
    <div>
      <div class="tabdiv"></div>
      <div class="transaction_in">
        <div class="transaction_in_right" style="width: 21rem;">
          <div>
            <div class="divTab" v-show="nowIndex === 0">
              <div class="Twokey w350">
                <span class="twokey_title">合约</span>
                <el-select class="" size="mini" @change="getPanelData" v-model="namesp" clearable placeholder="请选择">
                  <el-option
                    v-for="(item, inx) in ticket"
                    :key="inx"
                    :label="item.M + item.S + item.C"
                    :value="item.M + item.S + item.C">
                  </el-option>
                </el-select>

                <div class="twokey_right"></div>
              </div>
              <div class="Twokey_divin view">
                 <div class="Twokey view flex-row w350">
                    <div class="twokey2_title">数量</div>
                    <el-input class="twokey2_hands " v-model="Numberhand" type="number" name="number" value="1" min="1" size="mini"/>
                    <div class="m-l-5">手</div>
                  </div>
                  
                   <div class="view flex-row m-l-135">
                      <span style="color:#FF0000;">买:</span>
                      <span style="color: #000;">可开--  </span>
                      <span>可平 0</span>
                    </div>
                    <div class="view flex-row m-l-135">
                      <span style="color: #228B22;">卖:</span>
                      <span style="color: #000;">可开-- </span>
                      <span style="color: #000;">可平 0</span>
                    </div>
                  <div class="Twokey view flex-row">
                    <div class="m-l-20 fz-12 w50">价格： </div>
                    <el-select v-model="priceType" placeholder="请选择" size="mini">
                      <el-option
                        v-for="(item,dex) in pricechoose"
                        :key="dex"
                        :label="item.label"
                        :value="item.value">
                      </el-option>
                    </el-select>  
                   
                  </div>
               
                  <div style="display: flex;justify-content: flex-start;width: 100%;" class="view">
                    <div class="Twokey_isn_right">
                      <div class="reset1" @click="fuwei"><span>复 位</span></div>
                    </div>

                    <div class="Business view flex-row width100">
                      <div class="Business_left flex-1" @click="btnlist && business(selldis)">
                        <span class="Business_left_font1">{{ selldis || '- - ' }}</span>
                        <div class="Business_left_bor"></div>
                        <span class="Business_left_font1">买&nbsp;入</span>
                      </div>
                      <div class="Business_right flex-1" @click="btnlist && selldiss(buydis)">
                        <span class="Business_left_font2">{{ buydis || '- - ' }}</span>
                        <div class="Business_left_bor2"></div>
                        <span class="Business_left_font2">卖&nbsp;出</span>
                      </div>
                    </div>
                    
                  </div>
              </div>
              <div class="Business">
                <div class="Business_left" @click="btnlist && purchase(selldis)">
                  <span class="Business_left_font1">{{ selldis || '- - ' }}</span>
                  <div class="Business_left_bor1"></div>
                  <span class="Business_left_font1">买&nbsp;入</span>
                </div>
                <div class="Business_right" @click="btnlist && sellout(buydis)">
                  <span class="Business_left_font2">{{ buydis || '- - ' }}</span>
                  <div class="Business_left_bor1"></div>
                  <span class="Business_left_font2">卖&nbsp;出</span>
                </div>
                <div class="Business_right2" @click="btnlist && closeposition()">
                  <span class="Business_left_font3">优先平今</span>
                  <div class="Business_left_bor1"></div>
                  <span class="Business_left_font3">平&nbsp;仓</span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex';
export default {
  computed: mapState({
    select: state => state.trade.select /* 当前选中股指 */,
    symbol: state => state.trade.symbol /* 全部行情 */,
    symbos: state => state.trade.symbos /* 当前页面显示杨青 */,
    ticket: state => state.trade.ticket /* 推送数据 */,
    optionlist: state => state.trade.optionlist /* 全部合约代码 */,
    hoidlist: state => state.hoidlist /* 平仓ID */
  }),
  watch: {
    namesp(val) {
      for (let i = 0; i < this.list.length; i++) {
        if (val == this.list[i].code2) {
          return this.$store.commit('trade/select', this.list[i]);
        }
      }
    },
    hoidlist(val) {
      // console.log(val);
    },
    price(val) {
      // console.log(val);
      if (this.priceprict == false) {
        this.selldis = val; //卖价显示
        this.buydis = val;
      }
    },
    ticket(val) {
      if (this.priceprict == true) {
        this.price = ((val[this.select.code2] && val[this.select.code2].price) || 0).toFixed(this.select.digit);
        this.buydis = ((val[this.select.code2] && val[this.select.code2].price) || 0).toFixed(this.select.digit);
        // this.buydis = ((val[this.select.code2] && val[this.select.code2].bid) || 0).toFixed(this.select.digit);
        this.selldis = ((val[this.select.code2] && val[this.select.code2].price) || 0).toFixed(this.select.digit);
        // this.selldis = ((val[this.select.code2] && val[this.select.code2].ask) || 0).toFixed(this.select.digit);
      }
    },
    select(val) {
      this.priceprict = true;
      this.namesp = val.name_code;
      this.price = ((val[this.select.code2] && val[this.select.code2].price) || 0).toFixed(this.select.digit);
      // this.buydis = ((val[this.select.code2] && val[this.select.code2].bid) || 0).toFixed(this.select.digit);
      // this.selldis = ((val[this.select.code2] && val[this.select.code2].ask) || 0).toFixed(this.select.digit);
      this.buydis = ((val[this.select.code2] && val[this.select.code2].price) || 0).toFixed(this.select.digit);
      this.selldis = ((val[this.select.code2] && val[this.select.code2].price) || 0).toFixed(this.select.digit);
    }
  },
  created: function() {
    for (let i in this.optionlist) {
      this.list.push(this.optionlist[i]);
    }
    // console.log(this.list)
    this.namesp = this.select.name_code;
    this.price = ((this.ticket[this.select.code2] && this.ticket[this.select.code2].price) || 0).toFixed(this.select.digit);
    // this.buydis = ((this.ticket[this.select.code2] && this.ticket[this.select.code2].bid) || 0).toFixed(this.select.digit);
    // this.selldis = ((this.ticket[this.select.code2] && this.ticket[this.select.code2].adk) || 0).toFixed(this.select.digit);
    this.buydis = ((this.ticket[this.select.code2] && this.ticket[this.select.code2].price) || 0).toFixed(this.select.digit);
    this.selldis = ((this.ticket[this.select.code2] && this.ticket[this.select.code2].price) || 0).toFixed(this.select.digit);
  },
  data() {
    return {
      btnlist: true,
      list: [], //合约名称
      oneitem: {},
      // imags:'../assets/Navigation/1.png',
      nowIndex: 0,
      holdid: '', //平仓id
      namesp: '', //合约
      Numberhand: '', //手数
      price: '', //价格
      priceprict: true, //买卖价格变动
      profit_price: 0 /* 止盈价格 */,
      loss_price: 0 /* 止损价格 */,
      typename: '', //投机类型
      radioVal: '1', //开仓类型
      selldis: '', //卖价显示
      buydis: '', //买家显示
      disscond: false, //条件单
      typeList: [
        {
          name: '>=',
          id: 1
        },
        {
          name: '<=',
          id: 2
        }
      ] //方式
    };
  },
  components: {},
  mounted() {},
  methods: {
    change(listinfo) {
      // console.log(listinfo);
    },
    /* 切换select*/
    changeslect(code) {
      this.priceprict = true;
      for (let i = 0; i < this.list.length; i++) {
        if (code == this.list[i].code) {
          console.log(this.list[i]);
          return this.$store.commit('trade/select', this.list[i]);
        }
      }
    },
    /* 价格输入方式切换 */
    changeprice() {
      this.priceprict = false;
    },
    //买入
    purchase(num) {
      this.btnlist = false;
      if (this.Numberhand == '') {
        this.btnlist = true;
        return this.$notify.error('请输入手数');
      }
      if (this.priceprict == false) {
        if (this.selldis == this.price) {
          var buylist = {
            symbol_id: this.select.code2,
            cmd: 'NOW',
            rise_fall: 'RISE',
            price: num,
            volume: this.Numberhand,
            stop_profit_price: this.profit_price /* 止盈价格 */,
            stop_loss_price: this.loss_price /* 止损价格 */
          };
        } else {
          var buylist = {
            symbol_id: this.select.code2,
            cmd: 'NOW',
            rise_fall: 'RISE',
            // price: num,
            volume: this.Numberhand,
            stop_profit_price: this.profit_price /* 止盈价格 */,
            stop_loss_price: this.loss_price /* 止损价格 */
          };
        }
      } else {
        var buylist = {
          symbol_id: this.select.code2,
          cmd: 'NOW',
          rise_fall: 'RISE',
          // price: num,
          volume: this.Numberhand,
          stop_profit_price: this.profit_price /* 止盈价格 */,
          stop_loss_price: this.loss_price /* 止损价格 */
        };
      }
      var that = this;
      var bool = false;
      this.$layer.open({
        content: '确定买入' + this.Numberhand + '手' + this.namesp + ', 是否继续?',
        btn: ['确定', '取消'],
        yes: function(index, layero) {
          //按钮【按钮一】的回调
          if (!bool) {
            bool = true;

            that.$post('addons/tf-futures/order/open-with-price', buylist).then(res => {
              // console.log(res);
              that.$layer.closeAll();
              that.btnlist = true;
              bool = false;
              if (res.code == 200) {
                that.$notify.success(res.message || '开仓成功');
              } else {
                that.$notify.error(res.message || '开仓失败');
              }
            });
          }
        },
        btn2: function(index, layero) {
          that.$layer.closeAll();
          that.btnlist = true;
          that.$notify({
            type: 'info',
            message: '已取消购买'
          });
        },
        cancel: function() {
          //右上角关闭回调
          that.btnlist = true;
          that.$layer.closeAll();
          that.$layer.msg('已取消');
          //return false 开启该代码可禁止点击该按钮关闭
        }
      });
    },
    //卖出
    sellout(num) {
      this.btnlist = false;
      if (this.Numberhand == '') {
        this.btnlist = true;
        return this.$notify.error('请输入手数');
      }
      if (this.priceprict == false) {
        if (this.selldis == this.price) {
          var buylist = {
            symbol_id: this.select.code2,
            cmd: 'NOW',
            rise_fall: 'FALL',
            price: num,
            volume: this.Numberhand,
            stop_profit_price: this.profit_price /* 止盈价格 */,
            stop_loss_price: this.loss_price /* 止损价格 */
          };
        } else {
          var buylist = {
            symbol_id: this.select.code2,
            cmd: 'NOW',
            rise_fall: 'FALL',
            // price: num,
            volume: this.Numberhand,
            stop_profit_price: this.profit_price /* 止盈价格 */,
            stop_loss_price: this.loss_price /* 止损价格 */
          };
        }
      } else {
        var buylist = {
          symbol_id: this.select.code2,
          cmd: 'NOW',
          rise_fall: 'FALL',
          // price: num,
          volume: this.Numberhand,
          stop_profit_price: this.profit_price /* 止盈价格 */,
          stop_loss_price: this.loss_price /* 止损价格 */
        };
      }
      var that = this;
      var bool = false;
      this.$layer.open({
        content: '确定卖出' + this.Numberhand + '手' + this.namesp + ', 是否继续?',
        btn: ['确定', '取消'],
        yes: function(index, layero) {
          //按钮【按钮一】的回调
          if (!bool) {
            bool = true;

            that.$post('addons/tf-futures/order/open-with-price', buylist).then(res => {
              // console.log(res);
              that.$layer.closeAll();
              that.btnlist = true;
              bool = false;
              if (res.code == 200) {
                that.$notify.success(res.message || '开仓成功');
              } else {
                that.$notify.error(res.message || '开仓失败');
              }
            });
          }
        },
        btn2: function(index, layero) {
          that.$layer.closeAll();
          that.btnlist = true;
          that.$notify({
            type: 'info',
            message: '已取消购买'
          });
        },
        cancel: function() {
          //右上角关闭回调
          that.btnlist = true;
          that.$layer.closeAll();
          that.$layer.msg('已取消');
          //return false 开启该代码可禁止点击该按钮关闭
        }
      });
    },
    //平仓
    closeposition() {
      this.btnlist = false;
      if (this.hoidlist == null || this.hoidlist == '') {
        this.btnlist = true;
        return this.$notify.error('请选择要平仓的订单');
      }
      this.$post('addons/tf-futures/order/close-order', { orderid: this.hoidlist }).then(res => {
        // console.log(res);
        this.btnlist = true;
        if (res.code == 200) {
          this.$notify.success(res.message || '开仓成功');
        } else {
          this.$notify.error(res.message || '开仓失败');
        }
      });
    }
  }
};
</script>
<style scoped lang="less">
.tabs {
  height: 23px;
  line-height: 22px;
  color: #333333;
  display: flex;
  justify-content: flex-start;
  margin-top: 8px;
}

.li-tab {
  width: 80px;
  height: 23px;
  display: inline-block;
  text-align: center;
  font-size: 12px;
  margin-right: 3px;
  background-color: #c6d9f0;
  border: #a3bddc solid 1px;
  border-radius: 5px;
}

.divTab {
  width: 320px;
  /* height: 160px; */
  /* background-color: red; */
  /* margin-top: -0.95rem; */
}

.active {
  background: #ffffff;
  color: #333333;
}

.Twokey {
  width: 100%;
  height: 40px;
  display: flex;
  justify-content: flex-start;
  align-items: center;
}

.twokey_title {
  font-size: 12px;
  color: #333333;
  margin-left: 5%;
  margin-right: 16px;
}
.indiv {
  width: 100%;
  height: 32px;
  display: flex;
  justify-content: flex-start;
  font-size: 14px;
  margin-left: 16px;
  margin-top: 10px;
  color: #000000;
  align-items: center;
}
.Twokey_divin {
  width: 100%;
  /* height: 24px; */
  /* display: flex; */
  /* justify-content: flex-start; */
}

.twokey_inp {
  width: 50%;
  height: 22px;
}

.twokey_right {
  width: 6px;
  height: 22px;
  /* background-color: #BBBBBB; */
  /* border: #999999 solid 1px; */
  margin-left: 8px;
}

.radiodiv {
  padding-left: 32px;
  box-sizing: border-box;
  margin-top: 8px;
  margin-bottom: 8px;
  font-size: 12px;
}

.radiodiv_Purchase {
  color: #e15546;
  margin-left: 32px;
}

.radiodiv_Sellout {
  color: #3f9e0e;
}

.twokey_hands {
  width: 60%;
  height: 1.4rem;
  border-radius: 0.2rem;
  padding-left: 5px;
}

.Twokey_in {
  width: 100%;
  height: 2rem;
  /* background-color: #000000; */
  padding-left: 0.6rem;
  padding-right: 2rem;
  box-sizing: border-box;
  display: flex;
  justify-content: flex-start;
}

.Business {
  width: 100%;
  height: 4rem;
  /* background-color: #3F9E0E; */
  margin-top: 0.4rem;
  display: flex;
  justify-content: flex-start;
}

.Business_left {
  cursor: pointer;
  width: 105px;
  height: 70px;
  background-image: linear-gradient(to bottom, #ffffff, #f9f9f6, #f1f1eb);
  border-radius: 3px;
  margin-left: 5px;
  display: flex;
  justify-content: center;
  flex-direction: column;
  align-items: center;
  border: #003c74 solid 1px;
}

.Business_right {
  cursor: pointer;
  width: 105px;
  height: 70px;
  background-color: #f5f4ea;
  border-radius: 3px;
  margin-left: 10px;
  display: flex;
  justify-content: center;
  flex-direction: column;
  align-items: center;
  border: #003c74 solid 1px;
}
.Business_right2 {
  cursor: pointer;
  width: 105px;
  height: 70px;
  background-image: linear-gradient(to bottom, #ffffff, #f9f9f6, #f1f1eb);
  border-radius: 3px;
  margin-left: 10px;
  display: flex;
  justify-content: center;
  flex-direction: column;
  align-items: center;
  border: #003c74 solid 1px;
}
.Business_left_bor1 {
  width: 85px;
  height: 1px;
  background-color: #808080;
  margin-top: 3px;
  /* margin-bottom: 0.3rem; */
}
.Business_left_bor2 {
  width: 85px;
  height: 1px;
  background-color: #2d720b;
  margin-top: 3px;
  /* margin-bottom: 0.3rem; */
}
.Business_left_bor3 {
  width: 85px;
  height: 5px;
  background-color: #003c74;
  margin-top: 3px;
  /* margin-bottom: 0.3rem; */
}
.Business_left_font1 {
  color: #bd0909;
  font-size: 16px;
  font-weight: bold;
}
.Business_left_font2 {
  color: #088a02;
  font-size: 16px;
  font-weight: bold;
}
.Business_left_font3 {
  color: #000000;
  font-size: 16px;
  font-weight: bold;
}

.Twokey_isn_right {
  width: 40%;
  height: 5rem;
  /* background-color: #9d3b2f; */
}
.Twokey2 {
  width: 50%;
  height: 40px;
  float: left;
  display: flex;
  justify-content: flex-start;
  align-items: center;
}

.twokey2_title {
  font-size: 12px;
  color: #333333;
  margin-left: 5%;
  margin-right: 16px;
}
.twokey2_hands {
  width: 40%;
  height: 23px;
  border-radius: 2px;
  padding-left: 5px;
}

.isn_right_mai {
  width: 100%;
  height: 3rem;
  display: flex;
  justify-content: flex-start;
  align-items: center;
}

.isn_right_mai_left {
  width: 2rem;
  height: 2rem;
  border: #e15546 solid 1px;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #e15546;
  margin-left: 0.5rem;
}

.isn_right_mai_rissght {
  width: 2rem;
  height: 2rem;
  border: #3f9e0e solid 1px;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #3f9e0e;
  margin-left: 0.5rem;
}

.isn_right_mai_right {
  width: 6rem;
  height: 3rem;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding-left: 0.5rem;
  box-sizing: border-box;
  color: #333333;
  font-size: 0.8rem;
}

.reset {
  width: 70%;
  height: 1.5rem;
  border: #003c74 solid 1px;
  background-image: linear-gradient(to bottom, #fefefe, #f6f6f2, #e2dfd6);
  margin-left: 0.5rem;
  margin-top: 0.6rem;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #333333;
  font-size: 0.8rem;
  border-radius: 0.3rem;
}
/*条件单*/
.stoppo {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  /* background-color: red; */
  background-color: rgba(0, 0, 0, 0.2);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 101;
}
.bott_cha {
  width: 100%;
  height: 2rem;
  /* background-color: #00A2E8; */
  display: flex;
  justify-content: flex-start;
  align-items: center;
  padding-left: 1rem;
  box-sizing: border-box;
  margin-top: 0.5rem;
}
.butt_cha {
  cursor: pointer;
  width: 65px;
  height: 25px;
  background-image: linear-gradient(to bottom, #fefefe, #f6f6f2, #e2dfd6);
  border: #ccc solid 1px;
  margin-left: 8px;
  margin-top: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #333333;
  font-size: 12px;
  border-radius: 5px;
}
.butt_cha:hover {
  border-color: #003c74;
  button {
    color: #003c74;
  }
  
 }
</style>
